//
//  CustomPullRefresh.m
//  PullToRefresh
//
//  Created by lanou3g on 15/6/30.
//  Copyright (c) 2015年 Lanaya. All rights reserved.
//

#import "CustomPullRefresh.h"

@interface CustomPullRefresh ()

{
    int _currentColorIndex;      //当前正在发生动画One的下标
    int _currentLabelIndex;       //同上（颜色）
}

@property (nonatomic, retain) UIView *customView;          //.xib

@end

@implementation CustomPullRefresh

- (void)dealloc {
    [_refreshControl release];
    [_customView release];
    [_labelsArray release];
    [super dealloc];
}

- (UIRefreshControl *)refreshControl {
    if (!_refreshControl) {
        _refreshControl = [[UIRefreshControl alloc] init];
        _refreshControl.backgroundColor = [UIColor clearColor];
        _refreshControl.tintColor = [UIColor clearColor];
    }
    return _refreshControl;
}

- (instancetype) initWithTableView:(UITableView *) tableView {
    self = [super init];
    if (self) {
        
        _isAnimating = NO;
        _currentLabelIndex = 0;
        _currentColorIndex = 0;
        [tableView addSubview:self.refreshControl];
        [self loadCustomRefreshContents];
        
    }
    return self;
}

//加载.xib视图
- (void) loadCustomRefreshContents {
    NSArray *refreshContents = [[NSBundle mainBundle] loadNibNamed:@"CatPullToRefresh" owner:self options:nil];
    self.customView = refreshContents[0];
    self.customView.frame = self.refreshControl.bounds;
    
    self.labelsArray = [NSMutableArray array];
    for (int i = 0; i < self.customView.subviews.count; i++) {
        UILabel *label = (UILabel *)[self.customView viewWithTag:(i + 1)];
        [self.labelsArray addObject:label];
    }
    [self.refreshControl addSubview:self.customView];
}

#pragma mark - animation step

- (void) animationStepOne {
    _isAnimating = YES;
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        UILabel *label = self.labelsArray[_currentLabelIndex];
        label.transform = CGAffineTransformMakeRotation(M_PI_4);
        label.textColor = [self getNextColor];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            UILabel *label = self.labelsArray[_currentLabelIndex];
            label.transform = CGAffineTransformIdentity;
            label.textColor = [UIColor blackColor];
        } completion:^(BOOL finished) {
            _currentLabelIndex++;
            if (_currentLabelIndex < self.labelsArray.count) {
                [self animationStepOne];
            } else {
                [self animationStepTwo];
            }
        }];
    }];
    
}

- (void) animationStepTwo {
    [UIView animateWithDuration:0.35 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
        UILabel *label1 = self.labelsArray[0];
        UILabel *label2 = self.labelsArray[1];
        UILabel *label3 = self.labelsArray[2];
        UILabel *label4 = self.labelsArray[3];
        UILabel *label5 = self.labelsArray[4];
        UILabel *label6 = self.labelsArray[5];
        UILabel *label7 = self.labelsArray[6];
        label1.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label2.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label3.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label4.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label5.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label6.transform = CGAffineTransformMakeScale(1.5, 1.5);
        label7.transform = CGAffineTransformMakeScale(1.5, 1.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
            UILabel *label1 = self.labelsArray[0];
            UILabel *label2 = self.labelsArray[1];
            UILabel *label3 = self.labelsArray[2];
            UILabel *label4 = self.labelsArray[3];
            UILabel *label5 = self.labelsArray[4];
            UILabel *label6 = self.labelsArray[5];
            UILabel *label7 = self.labelsArray[6];
            label1.transform = CGAffineTransformIdentity;
            label2.transform = CGAffineTransformIdentity;
            label3.transform = CGAffineTransformIdentity;
            label4.transform = CGAffineTransformIdentity;
            label5.transform = CGAffineTransformIdentity;
            label6.transform = CGAffineTransformIdentity;
            label7.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            if (self.refreshControl.refreshing) {
                _currentLabelIndex = 0;
                [self animationStepOne];
            } else {
                _isAnimating = NO;
                _currentLabelIndex = 0;
                UILabel *label1 = self.labelsArray[0];
                UILabel *label2 = self.labelsArray[1];
                UILabel *label3 = self.labelsArray[2];
                UILabel *label4 = self.labelsArray[3];
                UILabel *label5 = self.labelsArray[4];
                UILabel *label6 = self.labelsArray[5];
                UILabel *label7 = self.labelsArray[6];
                label1.transform = CGAffineTransformIdentity;
                label2.transform = CGAffineTransformIdentity;
                label3.transform = CGAffineTransformIdentity;
                label4.transform = CGAffineTransformIdentity;
                label5.transform = CGAffineTransformIdentity;
                label6.transform = CGAffineTransformIdentity;
                label7.transform = CGAffineTransformIdentity;
                label1.textColor = [UIColor blackColor];
                label2.textColor = [UIColor blackColor];
                label3.textColor = [UIColor blackColor];
                label4.textColor = [UIColor blackColor];
                label5.textColor = [UIColor blackColor];
                label6.textColor = [UIColor blackColor];
                label7.textColor = [UIColor blackColor];
            }
        }];
    }];

}

- (UIColor *) getNextColor {

    NSArray *colorArray = @[[UIColor magentaColor], [UIColor brownColor], [UIColor yellowColor], [UIColor redColor], [UIColor greenColor], [UIColor blueColor], [UIColor orangeColor]];
    
    if (_currentColorIndex == self.labelsArray.count) {
        _currentColorIndex = 0;
    }
    
    UIColor *theColor = colorArray[_currentColorIndex];
    _currentColorIndex++;
    return theColor;
}

@end





















